﻿using System;
using System.Xml;

namespace HYTools
{
	public class AutoSaveSettingFileReadWriteKit
	{
		public static bool ReadMappingFile(string fileName, ref bool autoSave, ref double interval, ref int fileCount)
		{
			XmlDocument xmlDocument = new XmlDocument();
			try
			{
				xmlDocument.Load(fileName);
			}
			catch (Exception)
			{
				return false;
			}
			XmlElement xmlElement = xmlDocument.SelectSingleNode("AutoSaveSetting") as XmlElement;
			if (xmlElement == null)
			{
				return false;
			}
			autoSave = Convert.ToBoolean(xmlElement.GetAttribute("AutoSave"));
			interval = Convert.ToDouble(xmlElement.GetAttribute("Interval"));
			fileCount = Convert.ToInt32(xmlElement.GetAttribute("FileCount"));
			return true;
		}

		public static bool WriteMappingFile(string fileName, bool autoSave, double interval, int fileCount)
		{
			XmlDocument xmlDocument = new XmlDocument();
			XmlNode xmlNode = xmlDocument.CreateNode(XmlNodeType.XmlDeclaration, "", "");
			XmlNode xmlNode2 = xmlNode;
			xmlNode2.InnerText += " encoding=\"GB2312\"";
			xmlDocument.AppendChild(xmlNode);
			XmlElement xmlElement = xmlDocument.CreateElement("", "AutoSaveSetting", "");
			xmlElement.SetAttribute("AutoSave", autoSave.ToString());
			xmlElement.SetAttribute("Interval", interval.ToString());
			xmlElement.SetAttribute("FileCount", fileCount.ToString());
			xmlDocument.AppendChild(xmlElement);
			bool result;
			try
			{
				xmlDocument.Save(fileName);
				result = true;
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				result = false;
			}
			return result;
		}
	}
}
